﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using HY.Model;
//using Maticsoft.DBUtility;//Please add references
namespace HY.DAL.sys
{
    /// <summary>
    /// 数据访问类:sys_user
    /// </summary>
    public partial class sys_user
    {
        public sys_user()
        { }
        #region  Method


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HY.Model.sys.sys_user model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into sys_user(");
            strSql.Append("lgid,lgpw,user_name,sex,indate,birthday,outdate,school,edu,major,dept,status,inout,jg,hj_address,cur_address,code,tel,r_name,r_tel,QQ,sbh,job,email)");
            strSql.Append(" values (");
            strSql.Append("@lgid,@lgpw,@user_name,@sex,@indate,@birthday,@outdate,@school,@edu,@major,@dept,@status,@inout,@jg,@hj_address,@cur_address,@code,@tel,@r_name,@r_tel,@QQ,@sbh,@job,@email)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@lgid", SqlDbType.NVarChar,100),
					new SqlParameter("@lgpw", SqlDbType.NVarChar,100),
					new SqlParameter("@user_name", SqlDbType.NVarChar,100),
					new SqlParameter("@sex", SqlDbType.NVarChar,10),
					new SqlParameter("@indate", SqlDbType.Date,3),
					new SqlParameter("@birthday", SqlDbType.Date,3),
					new SqlParameter("@outdate", SqlDbType.Date,3),
					new SqlParameter("@school", SqlDbType.NVarChar,200),
					new SqlParameter("@edu", SqlDbType.NVarChar,100),
					new SqlParameter("@major", SqlDbType.NVarChar,100),
					new SqlParameter("@dept", SqlDbType.NVarChar,100),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@inout", SqlDbType.Int,4),
					new SqlParameter("@jg", SqlDbType.NVarChar,100),
					new SqlParameter("@hj_address", SqlDbType.NVarChar,200),
					new SqlParameter("@cur_address", SqlDbType.NVarChar,200),
					new SqlParameter("@code", SqlDbType.NVarChar,100),
					new SqlParameter("@tel", SqlDbType.NVarChar,100),
					new SqlParameter("@r_name", SqlDbType.NVarChar,100),
					new SqlParameter("@r_tel", SqlDbType.NVarChar,100),
					new SqlParameter("@QQ", SqlDbType.NVarChar,100),
					new SqlParameter("@sbh", SqlDbType.NVarChar,100),
                    new SqlParameter("@job", SqlDbType.NVarChar,100),
                    new SqlParameter("@email", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.lgid;
            parameters[1].Value = model.lgpw;
            parameters[2].Value = model.user_name;
            parameters[3].Value = model.sex;
            parameters[4].Value = model.indate;
            parameters[5].Value = model.birthday;
            parameters[6].Value = model.outdate;
            parameters[7].Value = model.school;
            parameters[8].Value = model.edu;
            parameters[9].Value = model.major;
            parameters[10].Value = model.dept;
            parameters[11].Value = model.status;
            parameters[12].Value = model.inout;
            parameters[13].Value = model.jg;
            parameters[14].Value = model.hj_address;
            parameters[15].Value = model.cur_address;
            parameters[16].Value = model.code;
            parameters[17].Value = model.tel;
            parameters[18].Value = model.r_name;
            parameters[19].Value = model.r_tel;
            parameters[20].Value = model.QQ;
            parameters[21].Value = model.sbh;
            parameters[22].Value = model.job;
            parameters[23].Value = model.email;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(HY.Model.sys.sys_user model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sys_user set ");
            strSql.Append("lgid=@lgid,");
            strSql.Append("lgpw=@lgpw,");
            strSql.Append("user_name=@user_name,");
            strSql.Append("sex=@sex,");
            strSql.Append("indate=@indate,");
            strSql.Append("birthday=@birthday,");
            strSql.Append("outdate=@outdate,");
            strSql.Append("school=@school,");
            strSql.Append("edu=@edu,");
            strSql.Append("major=@major,");
            strSql.Append("dept=@dept,");
            strSql.Append("status=@status,");
            strSql.Append("inout=@inout,");
            strSql.Append("jg=@jg,");
            strSql.Append("hj_address=@hj_address,");
            strSql.Append("cur_address=@cur_address,");
            strSql.Append("code=@code,");
            strSql.Append("tel=@tel,");
            strSql.Append("r_name=@r_name,");
            strSql.Append("r_tel=@r_tel,");
            strSql.Append("QQ=@QQ,");
            strSql.Append("email=@email");
            strSql.Append("sbh=@sbh");
            strSql.Append("job=@job");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@lgid", SqlDbType.NVarChar,100),
					new SqlParameter("@lgpw", SqlDbType.NVarChar,100),
					new SqlParameter("@user_name", SqlDbType.NVarChar,100),
					new SqlParameter("@sex", SqlDbType.NVarChar,10),
					new SqlParameter("@indate", SqlDbType.Date,3),
					new SqlParameter("@birthday", SqlDbType.Date,3),
					new SqlParameter("@outdate", SqlDbType.Date,3),
					new SqlParameter("@school", SqlDbType.NVarChar,200),
					new SqlParameter("@edu", SqlDbType.NVarChar,100),
					new SqlParameter("@major", SqlDbType.NVarChar,100),
					new SqlParameter("@dept", SqlDbType.NVarChar,100),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@inout", SqlDbType.Int,4),
					new SqlParameter("@jg", SqlDbType.NVarChar,100),
					new SqlParameter("@hj_address", SqlDbType.NVarChar,200),
					new SqlParameter("@cur_address", SqlDbType.NVarChar,200),
					new SqlParameter("@code", SqlDbType.NVarChar,100),
					new SqlParameter("@tel", SqlDbType.NVarChar,100),
					new SqlParameter("@r_name", SqlDbType.NVarChar,100),
					new SqlParameter("@r_tel", SqlDbType.NVarChar,100),
					new SqlParameter("@QQ", SqlDbType.NVarChar,100),
					new SqlParameter("@email", SqlDbType.NVarChar,100),
                    		new SqlParameter("@sbh", SqlDbType.NVarChar,100),
                            		new SqlParameter("@job", SqlDbType.NVarChar,100),
					new SqlParameter("@sid", SqlDbType.Int,4)};
            parameters[0].Value = model.lgid;
            parameters[1].Value = model.lgpw;
            parameters[2].Value = model.user_name;
            parameters[3].Value = model.sex;
            parameters[4].Value = model.indate;
            parameters[5].Value = model.birthday;
            parameters[6].Value = model.outdate;
            parameters[7].Value = model.school;
            parameters[8].Value = model.edu;
            parameters[9].Value = model.major;
            parameters[10].Value = model.dept;
            parameters[11].Value = model.status;
            parameters[12].Value = model.inout;
            parameters[13].Value = model.jg;
            parameters[14].Value = model.hj_address;
            parameters[15].Value = model.cur_address;
            parameters[16].Value = model.code;
            parameters[17].Value = model.tel;
            parameters[18].Value = model.r_name;
            parameters[19].Value = model.r_tel;
            parameters[20].Value = model.QQ;
            parameters[21].Value = model.email;
            parameters[22].Value = model.sbh;
            parameters[23].Value = model.job;
            parameters[24].Value = model.sid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int sid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sys_user ");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
            parameters[0].Value = sid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from sys_user ");
            strSql.Append(" where sid in (" + sidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HY.Model.sys.sys_user GetModel(int sid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sid,lgid,job,sbh,lgpw,user_name,sex,indate,birthday,outdate,school,edu,major,dept,status,inout,jg,hj_address,cur_address,code,tel,r_name,r_tel,QQ,email from sys_user ");
            strSql.Append(" where sid=@sid");
            SqlParameter[] parameters = {
					new SqlParameter("@sid", SqlDbType.Int,4)
			};
            parameters[0].Value = sid;

            HY.Model.sys.sys_user model = new HY.Model.sys.sys_user();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sid"] != null && ds.Tables[0].Rows[0]["sid"].ToString() != "")
                {
                    model.sid = int.Parse(ds.Tables[0].Rows[0]["sid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["lgid"] != null && ds.Tables[0].Rows[0]["lgid"].ToString() != "")
                {
                    model.lgid = ds.Tables[0].Rows[0]["lgid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["lgpw"] != null && ds.Tables[0].Rows[0]["lgpw"].ToString() != "")
                {
                    model.lgpw = ds.Tables[0].Rows[0]["lgpw"].ToString();
                }
                if (ds.Tables[0].Rows[0]["user_name"] != null && ds.Tables[0].Rows[0]["user_name"].ToString() != "")
                {
                    model.user_name = ds.Tables[0].Rows[0]["user_name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sex"] != null && ds.Tables[0].Rows[0]["sex"].ToString() != "")
                {
                    model.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                }
                if (ds.Tables[0].Rows[0]["indate"] != null && ds.Tables[0].Rows[0]["indate"].ToString() != "")
                {
                    model.indate = DateTime.Parse(ds.Tables[0].Rows[0]["indate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["birthday"] != null && ds.Tables[0].Rows[0]["birthday"].ToString() != "")
                {
                    model.birthday = DateTime.Parse(ds.Tables[0].Rows[0]["birthday"].ToString());
                }
                if (ds.Tables[0].Rows[0]["outdate"] != null && ds.Tables[0].Rows[0]["outdate"].ToString() != "")
                {
                    model.outdate = DateTime.Parse(ds.Tables[0].Rows[0]["outdate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["school"] != null && ds.Tables[0].Rows[0]["school"].ToString() != "")
                {
                    model.school = ds.Tables[0].Rows[0]["school"].ToString();
                }
                if (ds.Tables[0].Rows[0]["edu"] != null && ds.Tables[0].Rows[0]["edu"].ToString() != "")
                {
                    model.edu = ds.Tables[0].Rows[0]["edu"].ToString();
                }
                if (ds.Tables[0].Rows[0]["major"] != null && ds.Tables[0].Rows[0]["major"].ToString() != "")
                {
                    model.major = ds.Tables[0].Rows[0]["major"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dept"] != null && ds.Tables[0].Rows[0]["dept"].ToString() != "")
                {
                    model.dept = ds.Tables[0].Rows[0]["dept"].ToString();
                }
                if (ds.Tables[0].Rows[0]["status"] != null && ds.Tables[0].Rows[0]["status"].ToString() != "")
                {
                    model.status = int.Parse(ds.Tables[0].Rows[0]["status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["inout"] != null && ds.Tables[0].Rows[0]["inout"].ToString() != "")
                {
                    model.inout = int.Parse(ds.Tables[0].Rows[0]["inout"].ToString());
                }
                if (ds.Tables[0].Rows[0]["jg"] != null && ds.Tables[0].Rows[0]["jg"].ToString() != "")
                {
                    model.jg = ds.Tables[0].Rows[0]["jg"].ToString();
                }
                if (ds.Tables[0].Rows[0]["hj_address"] != null && ds.Tables[0].Rows[0]["hj_address"].ToString() != "")
                {
                    model.hj_address = ds.Tables[0].Rows[0]["hj_address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["cur_address"] != null && ds.Tables[0].Rows[0]["cur_address"].ToString() != "")
                {
                    model.cur_address = ds.Tables[0].Rows[0]["cur_address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["code"] != null && ds.Tables[0].Rows[0]["code"].ToString() != "")
                {
                    model.code = ds.Tables[0].Rows[0]["code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["tel"] != null && ds.Tables[0].Rows[0]["tel"].ToString() != "")
                {
                    model.tel = ds.Tables[0].Rows[0]["tel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["r_name"] != null && ds.Tables[0].Rows[0]["r_name"].ToString() != "")
                {
                    model.r_name = ds.Tables[0].Rows[0]["r_name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["r_tel"] != null && ds.Tables[0].Rows[0]["r_tel"].ToString() != "")
                {
                    model.r_tel = ds.Tables[0].Rows[0]["r_tel"].ToString();
                }
                if (ds.Tables[0].Rows[0]["QQ"] != null && ds.Tables[0].Rows[0]["QQ"].ToString() != "")
                {
                    model.QQ = ds.Tables[0].Rows[0]["QQ"].ToString();
                }
                if (ds.Tables[0].Rows[0]["email"] != null && ds.Tables[0].Rows[0]["email"].ToString() != "")
                {
                    model.email = ds.Tables[0].Rows[0]["email"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sbh"] != null && ds.Tables[0].Rows[0]["sbh"].ToString() != "")
                {
                    model.sbh = ds.Tables[0].Rows[0]["sbh"].ToString();
                }

                if (ds.Tables[0].Rows[0]["job"] != null && ds.Tables[0].Rows[0]["job"].ToString() != "")
                {
                    model.job = ds.Tables[0].Rows[0]["job"].ToString();
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sid,sbh,job,lgid,lgpw,user_name,sex,indate,birthday,outdate,school,edu,major,dept,status,inout,jg,hj_address,cur_address,code,tel,r_name,r_tel,QQ,email ");
            strSql.Append(" FROM sys_user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sid,sbh,job,lgid,lgpw,user_name,sex,indate,birthday,outdate,school,edu,major,dept,status,inout,jg,hj_address,cur_address,code,tel,r_name,r_tel,QQ,email ");
            strSql.Append(" FROM sys_user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM sys_user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.sid desc");
            }
            strSql.Append(")AS Row, T.*  from sys_user T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.NVarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.NVarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.NVarChar,1000),
                    };
            parameters[0].Value = "sys_user";
            parameters[1].Value = "sid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

